package com.hqyj.repertory.controller;
import com.hqyj.repertory.model.OutRepertory;
import com.hqyj.repertory.model.Repertory;
import frame.repertory.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;

@Controller
@RequestMapping("/repertoryController")
public class OutRepertoryController extends BaseController {
    @RequestMapping("/out.do")
    public String outRepertoryList(HttpServletRequest request, HttpServletResponse response, Model model){
        ArrayList<OutRepertory> outRepertory_list = outRepertoryService.queryAllOutRepertory();
        System.out.println(outRepertory_list);
        model.addAttribute("outRepertory_list",outRepertory_list);
        return "/view/repertory/outRepertory/outRepertoryList";
    }
    @RequestMapping("/getOutRepertory.do")
    public String queryOutRepertoryById(HttpServletRequest request, HttpServletResponse response, Model model,String idStr){

        OutRepertory outRepertory = outRepertoryService.queryOutRepertoryById(Integer.parseInt(idStr));

        model.addAttribute("outRepertory",outRepertory);

        return "/view/repertory/outRepertory/updateOutRepertory";

    }
    @RequestMapping("/updateOutRepertory.do")
    public String updateOutRepertory(HttpServletRequest request, HttpServletResponse response, Model model,OutRepertory outRepertory){
        outRepertoryService.updateOutRepertory(outRepertory);
        ArrayList<OutRepertory> outRepertory_list = outRepertoryService.queryAllOutRepertory();
        model.addAttribute("outRepertory_list",outRepertory_list);
        return "/view/repertory/outRepertory/outRepertoryList";
    }
    @RequestMapping("/outRepertoryCondition.do")
    public String utRepertoryCondition(HttpServletRequest request, HttpServletResponse response, Model model){
        ArrayList<OutRepertory> outRepertory_list = outRepertoryService.queryOutRepertoryCondition();
        System.out.println(outRepertory_list);
        if(outRepertory_list.size()>0){
            model.addAttribute("message","以下物品还未出库，请尽快出库");
            model.addAttribute("outRepertory_list",outRepertory_list);

        }else{
            model.addAttribute("message","没有未出库物品哦，试试其他操作吧");
            outRepertory_list = outRepertoryService.queryAllOutRepertory();
            model.addAttribute("outRepertory_list",outRepertory_list);
        }
        return "/view/repertory/outRepertory/outRepertoryList";
    }
    @RequestMapping("goOutRepertory.do")
    public String goOutRepertory(HttpServletRequest request, HttpServletResponse response, Model model,String id,String name,String number){
        OutRepertory outRepertory = new OutRepertory();
        outRepertory.setId(Integer.parseInt(id));
        outRepertory.setProductMaterialsName(name);
        outRepertory.setOutRepertoryNumber(Integer.parseInt(number));
        outRepertory.setOutRepertoryDate(new Date());
        ArrayList<Repertory> repertories = repertoryService.queryAllRepertory();
        for (Repertory repertory:repertories){
            if(repertory.getProductMaterialsName().equals(name)){
                outRepertory.setOutRepertoryId(repertory.getId());
            }
        }
        Repertory repertory = repertoryService.queryRepertoryByProductMaterialsName(name);
        if(repertory == null){
            model.addAttribute("message","该物品不在仓库，请先采购哦");
            ArrayList<OutRepertory> outRepertory_list = outRepertoryService.queryAllOutRepertory();
            model.addAttribute("outRepertory_list",outRepertory_list);
            return "/view/repertory/outRepertory/outRepertoryList";
        }
        outRepertoryService.updateOutRepertoryExceptType(outRepertory);
        repertory.setNumber(repertory.getNumber()-Integer.parseInt(number));
        repertoryService.updateRepertory(repertory);
        ArrayList<OutRepertory> outRepertory_list = outRepertoryService.queryAllOutRepertory();
        model.addAttribute("message","出库成功");
        model.addAttribute("outRepertory_list",outRepertory_list);
        return "/view/repertory/outRepertory/outRepertoryList";
    }
}
